﻿using System;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class AddEditHistory : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            this.Cancel += AddEditHistory_Cancel;
            base.OnInit(e);
        }

        void AddEditHistory_Cancel(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, this.ItemID, "AddEditItem");
            Response.Redirect(url);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            History history = History.SelectBy_CooperativeID_Year(this.ItemID, Convert.ToInt32(ddlHistoryYear.SelectedValue));
            if (history != null)
            {
                history.Delete();

                // Clear data on controls.

                txtCharterCapital.Text = string.Empty;
                txtWorkingCapital.Text = string.Empty;
                txtFixedCapital.Text = string.Empty;
                txtTurnover.Text = string.Empty;
                txtTax.Text = string.Empty;
                txtProfit.Text = string.Empty;
                // Số lượng nhân sự
                txtLabourQuantity.Text = string.Empty;
                txtWomanLabourQuantity.Text = string.Empty;
                txtCooperatorQuantity.Text = string.Empty;
                txtWomanCooperatorQuantity.Text = string.Empty;
                ddlRank.SelectedIndex = 0;

                // Tổ chức / Đoàn thể
                txtPartyMemberQuantity.Text = string.Empty;
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            bool insert = false;
            History history = History.SelectBy_CooperativeID_Year(this.ItemID, Convert.ToInt32(ddlHistoryYear.SelectedValue));
            if (history == null)
            {
                history = new History();
                insert = true;
            }

            if (txtCharterCapital.Text.Trim().Length != 0)
            {
                history.CharterCapital = Decimal.Parse(txtCharterCapital.Text);
            }

            if (txtWorkingCapital.Text.Trim().Length != 0)
            {
                history.WorkingCapital = Decimal.Parse(txtWorkingCapital.Text);
            }

            if (txtFixedCapital.Text.Trim().Length != 0)
            {
                history.FixedCapital = Decimal.Parse(txtFixedCapital.Text);
            }

            if (txtTurnover.Text.Trim().Length != 0)
            {
                history.Turnover = Decimal.Parse(txtTurnover.Text);
            }

            if (txtTax.Text.Trim().Length != 0)
            {
                history.Tax = Decimal.Parse(txtTax.Text);
            }

            if (txtProfit.Text.Trim().Length != 0)
            {
                history.Profit = Decimal.Parse(txtProfit.Text);
            }
            // Số lượng nhân sự
            if (txtLabourQuantity.Text.Trim().Length != 0)
            {
                history.LabourQuantity = Int32.Parse(txtLabourQuantity.Text);
            }

            if (txtCooperatorQuantity.Text.Trim().Length != 0)
            {
                history.CooperatorQuantity = Int32.Parse(txtCooperatorQuantity.Text);
            }

            if (txtWomanLabourQuantity.Text.Trim().Length != 0)
            {
                history.WomanLabourQuantity = Int32.Parse(txtWomanLabourQuantity.Text);
            }

            if (txtWomanCooperatorQuantity.Text.Trim().Length != 0)
            {
                history.WomanCooperatorQuantity = Int32.Parse(txtWomanCooperatorQuantity.Text);
            }
            // Xếp loại
            history.Rank = ddlRank.SelectedValue;


            // Tổ chức / Đoàn thể
            history.PartyMemberQuantity = Convert.ToInt32(txtPartyMemberQuantity.Text);

            history.CooperativeID = this.ItemID;
            history.Year = Convert.ToInt32(ddlHistoryYear.SelectedValue);

            if (insert)
            {
                this.ItemID = history.Insert();
            }
            else
            {
                history.Update();
            }

            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "CooperativeManager");
            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                for (int i = 1975; i <= DateTime.Today.Year; i++)
                {
                    ddlHistoryYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                ddlHistoryYear.SelectedValue = DateTime.Today.Year.ToString();

                Cooperative cooperative = Cooperative.Load(this.ItemID);
                txtTitle.Text = cooperative.Title;

                if (this.ItemID > 0)
                {
                    DisplayHistoryData(Convert.ToInt32(ddlHistoryYear.SelectedValue));
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        protected void ddlHistoryYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayHistoryData(Convert.ToInt32(ddlHistoryYear.SelectedValue));
        }

        private void DisplayHistoryData(int year)
        {
            History history = History.SelectBy_CooperativeID_Year(this.ItemID, year);
            if (history != null)
            {
                txtCharterCapital.Text = history.CharterCapital.ToString("N0");
                txtWorkingCapital.Text = history.WorkingCapital.ToString("N0");
                txtFixedCapital.Text = history.FixedCapital.ToString("N0");
                txtTurnover.Text = history.Turnover.ToString("N0");
                txtTax.Text = history.Tax.ToString("N0");
                txtProfit.Text = history.Profit.ToString("N0");
                // Số lượng nhân sự
                txtLabourQuantity.Text = history.LabourQuantity.ToString("N0");
                txtWomanLabourQuantity.Text = history.WomanLabourQuantity.ToString("N0");
                txtCooperatorQuantity.Text = history.CooperatorQuantity.ToString("N0");
                txtWomanCooperatorQuantity.Text = history.WomanCooperatorQuantity.ToString("N0");
                ddlRank.SelectedValue = history.Rank;

                // Tổ chức / Đoàn thể
                txtPartyMemberQuantity.Text = history.PartyMemberQuantity.ToString("N0");
            }
            else
            {
                txtCharterCapital.Text = string.Empty;
                txtWorkingCapital.Text = string.Empty;
                txtFixedCapital.Text = string.Empty;
                txtTurnover.Text = string.Empty;
                txtTax.Text = string.Empty;
                txtProfit.Text = string.Empty;
                // Số lượng nhân sự
                txtLabourQuantity.Text = string.Empty;
                txtWomanLabourQuantity.Text = string.Empty;
                txtCooperatorQuantity.Text = string.Empty;
                txtWomanCooperatorQuantity.Text = string.Empty;
                ddlRank.SelectedIndex = 0;

                // Tổ chức / Đoàn thể
                txtPartyMemberQuantity.Text = string.Empty;
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            Session["LMHTX.History.Clipboard"] = Convert.ToInt32(ddlHistoryYear.SelectedValue);
        }

        protected void btnPaste_Click(object sender, EventArgs e)
        {
            int year = (int)Session["LMHTX.History.Clipboard"];
            DisplayHistoryData(year);
        }
    }
}